package com.dk.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dk.pojo.Menu;
import com.dk.pojo.User;
import com.dk.service.MenuService;

@Controller
@RequestMapping("menuController")
public class MenuController {

	@Autowired
	private MenuService menuService;
	
	//��ѯ���˵�
	@RequestMapping("getMenu")
	@ResponseBody
	public List<Map<String, Object>> getMenu(HttpServletRequest request){
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("u");
		//�����û���ȡ
		
		List<Map<String, Object>> menus = menuService.getMenu(user);
		return menus;
	}
	
	//��ת���˵�����ҳ��
	@RequestMapping("getTree")
	public String showTree() {
		
		return "menu/showTree";
	}
	//��ѯZTree��ʽ�Ĳ˵�
	@RequestMapping("getZTree")
	@ResponseBody
	public List<Map<String, Object>> getZTree(){
		List<Map<String, Object>> ZTree = menuService.getZTree();
		return ZTree;
	}
	//��ת����ҳ��
	@RequestMapping("toAdd")
	public String toAdd() {
		return "menu/add";
	}
	//����
	@RequestMapping("addMenu")
	@ResponseBody
	public String addMenu(Menu menu){
		menuService.addMenu(menu);
		return "1";
	}
	//����
	@RequestMapping("toUpdate/{id}")
	public String toUpdate(@PathVariable("id") Integer id, Model model) {
		Menu menu = menuService.findMenuById(id);
		model.addAttribute("menu", menu);
		return "menu/update";
	}
	//�޸�
	@RequestMapping("updateMenu")
	@ResponseBody
	public String updateMenu(Menu menu) {
		menuService.updateMenu(menu);
		return "1";
	}
	//ɾ��
	@RequestMapping("deleteMenu/{id}")
	@ResponseBody
	public String deleteMenu(@PathVariable("id") Integer id) {
		try {
			menuService.deleteMenu(id);
			return "1";
		} catch (Exception e) {
			e.printStackTrace();
			return "0";
		}
	}
	
	//��ɫ��ṹ���ɫ����
	@RequestMapping("getCheckedZTree/{roleId}")
	@ResponseBody
	public List<Map<String, Object>> getCheckedZTree(@PathVariable("roleId") Integer roleId){
		List<Map<String, Object>> checked = menuService.getCheckedZTree(roleId);
		return checked;
	}
}
